package com.dd.demo01.config;

import com.dd.demo01.po.TestBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author: lkd
 * @date: 2023/04/24/1:23
 * @Description:
 */
public class ApplicationContextInitializerTest implements ApplicationContextInitializer<ConfigurableApplicationContext> {

	/**
	 * 容器初始化前操作,refresh()方法之前执行
	 *
	 * @param applicationContext IOC容器，如XmlWebApplicationContext
	 */
	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		// 添加一个BeanDefinitionRegistryPostProcessor
		applicationContext.addBeanFactoryPostProcessor(new BeanDefinitionRegistryPostProcessor() {

			@Override
			public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
				System.out.println("自定义 BeanDefinitionRegistryPostProcessor   postProcessBeanDefinitionRegistry方法执行");
				DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) registry;
				// 手动注册 BeanFactoryPostProcessor
				System.out.println("ApplicationContextInitializerTest---手动注册了---beanFactoryPostProcessorTest");
				beanFactory.registerBeanDefinition("beanFactoryPostProcessorTest", new RootBeanDefinition(BeanFactoryPostProcessorTest.class));
			}

			@Override
			public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
				System.out.println("自定义 BeanDefinitionRegistryPostProcessor   postProcessBeanFactory");
				// 手动注册BeanPostProcessor
				System.out.println("ApplicationContextInitializerTest--- 手动添加了--- 一个BeanPostProcessor");
				beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
					@Override
					public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
						if (beanName.equals("phone") || beanName.equals("testBeanDefinition") || beanName.equals("user")) {
							System.out.println(beanName + " ---  执行我注册的beanDefinition  ---- postProcessBeforeInitialization");
						}
						return bean;
					}

					@Override
					public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
						if (beanName.equals("phone") || beanName.equals("testBeanDefinition") || beanName.equals("user")) {
							System.out.println(beanName + " --- 执行我注册的beanDefinition ---  postProcessAfterInitialization");
						}
						return bean;
					}
				});
			}
		});
		System.out.println("我在refresh()之前执行");
		System.out.println(applicationContext);
	}

}
